package com.dc.trans.steps.dataunpack;

import org.pentaho.di.core.Const;
import org.pentaho.di.core.exception.KettleValueException;
import org.pentaho.di.core.row.ValueMeta;
import org.pentaho.di.core.xml.XMLHandler;
import org.pentaho.di.i18n.BaseMessages;
import org.w3c.dom.Node;

public class DataUnPackField implements Cloneable {
	
	private static Class<?> PKG = DataUnPackField.class;

	public final static int TYPE_TRIM_NONE = 0;
	public final static int TYPE_TRIM_LEFT = 1;
	public final static int TYPE_TRIM_RIGHT = 2;
	public final static int TYPE_TRIM_BOTH = 3;

	public final static String trimTypeCode[] = { "none", "left", "right", "both" };

	public final static String trimTypeDesc[] = { BaseMessages.getString(PKG,"DataUnPackField.TrimType.None"),
		BaseMessages.getString(PKG,"DataUnPackField.TrimType.Left"), BaseMessages.getString(PKG,"DataUnPackField.TrimType.Right"),
		BaseMessages.getString(PKG,"DataUnPackField.TrimType.Both") };

	public final static String POSITION_MARKER = ",";

	private String name;

	private int type;
	private int length;
	private String format;
	private int trimtype;
	private int precision;
	private String currencySymbol;
	private String decimalSymbol;
	private String groupSymbol;
	private boolean repeat;

	private String samples[];

	public DataUnPackField(String fieldname) {
		this.name = fieldname;
		this.length = -1;
		this.type = ValueMeta.TYPE_STRING;
		this.format = "";
		this.trimtype = TYPE_TRIM_NONE;
		this.groupSymbol = "";
		this.decimalSymbol = "";
		this.currencySymbol = "";
		this.precision = -1;
		this.repeat = false;
	}

	public DataUnPackField() {
		this((String) null);
	}

	public String getXML() {
		String retval = "";

		retval += "      <field>" + Const.CR;
		retval += "        " + XMLHandler.addTagValue("name", getName());
		retval += "        " + XMLHandler.addTagValue("type", getTypeDesc());
		retval += "        " + XMLHandler.addTagValue("format", getFormat());
		retval += "        " + XMLHandler.addTagValue("currency", getCurrencySymbol());
		retval += "        " + XMLHandler.addTagValue("decimal", getDecimalSymbol());
		retval += "        " + XMLHandler.addTagValue("group", getGroupSymbol());
		retval += "        " + XMLHandler.addTagValue("length", getLength());
		retval += "        " + XMLHandler.addTagValue("precision", getPrecision());
		retval += "        " + XMLHandler.addTagValue("trim_type", getTrimTypeCode());
		retval += "        " + XMLHandler.addTagValue("repeat", isRepeated());
		retval += "        </field>" + Const.CR;

		return retval;
	}

	public DataUnPackField(Node fnode) throws KettleValueException {
		setName(XMLHandler.getTagValue(fnode, "name"));
		setType(ValueMeta.getType(XMLHandler.getTagValue(fnode, "type")));
		setFormat(XMLHandler.getTagValue(fnode, "format"));
		setCurrencySymbol(XMLHandler.getTagValue(fnode, "currency"));
		setDecimalSymbol(XMLHandler.getTagValue(fnode, "decimal"));
		setGroupSymbol(XMLHandler.getTagValue(fnode, "group"));
		setLength(Const.toInt(XMLHandler.getTagValue(fnode, "length"), -1));
		setPrecision(Const.toInt(XMLHandler.getTagValue(fnode, "precision"), -1));
		setTrimType(getTrimTypeByCode(XMLHandler.getTagValue(fnode, "trim_type")));
		setRepeated(!"N".equalsIgnoreCase(XMLHandler.getTagValue(fnode, "repeat")));
	}

	public final static int getTrimTypeByCode(String tt) {
		if (tt == null)
			return 0;

		for (int i = 0; i < trimTypeCode.length; i++) {
			if (trimTypeCode[i].equalsIgnoreCase(tt))
				return i;
		}
		return 0;
	}

	public final static int getTrimTypeByDesc(String tt) {
		if (tt == null)
			return 0;

		for (int i = 0; i < trimTypeDesc.length; i++) {
			if (trimTypeDesc[i].equalsIgnoreCase(tt))
				return i;
		}
		return 0;
	}

	public final static String getTrimTypeCode(int i) {
		if (i < 0 || i >= trimTypeCode.length)
			return trimTypeCode[0];
		return trimTypeCode[i];
	}

	public final static String getTrimTypeDesc(int i) {
		if (i < 0 || i >= trimTypeDesc.length)
			return trimTypeDesc[0];
		return trimTypeDesc[i];
	}

	public Object clone() {
		try {
			DataUnPackField retval = (DataUnPackField) super.clone();
			return retval;
		} catch (CloneNotSupportedException e) {
			return null;
		}
	}

	public int getLength() {
		return length;
	}

	public void setLength(int length) {
		this.length = length;
	}

	public String getName() {
		return name;
	}

	public void setName(String fieldname) {
		this.name = fieldname;
	}

	public int getType() {
		return type;
	}

	public String getTypeDesc() {
		return ValueMeta.getTypeDesc(type);
	}

	public void setType(int type) {
		this.type = type;
	}

	public String getFormat() {
		return format;
	}

	public void setFormat(String format) {
		this.format = format;
	}

	public void setSamples(String samples[]) {
		this.samples = samples;
	}

	public String[] getSamples() {
		return samples;
	}

	public int getTrimType() {
		return trimtype;
	}

	public String getTrimTypeCode() {
		return getTrimTypeCode(trimtype);
	}

	public String getTrimTypeDesc() {
		return getTrimTypeDesc(trimtype);
	}

	public void setTrimType(int trimtype) {
		this.trimtype = trimtype;
	}

	public String getGroupSymbol() {
		return groupSymbol;
	}

	public void setGroupSymbol(String group_symbol) {
		this.groupSymbol = group_symbol;
	}

	public String getDecimalSymbol() {
		return decimalSymbol;
	}

	public void setDecimalSymbol(String decimal_symbol) {
		this.decimalSymbol = decimal_symbol;
	}

	public String getCurrencySymbol() {
		return currencySymbol;
	}

	public void setCurrencySymbol(String currency_symbol) {
		this.currencySymbol = currency_symbol;
	}

	public int getPrecision() {
		return precision;
	}

	public void setPrecision(int precision) {
		this.precision = precision;
	}

	public boolean isRepeated() {
		return repeat;
	}

	public void setRepeated(boolean repeat) {
		this.repeat = repeat;
	}

	public void flipRepeated() {
		repeat = !repeat;
	}

	public void guess() {
	}

}
